package org.jeecgframework.web.system.pojo.base;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.ExcelUtil;

/**
 * @author xiul
 * 2016-6-6 下午5:08:15
 * Description 
 */
public class UploadChangeRecordThread extends Thread{
	
	private static SystemService systemService = ApplicationContextUtil.getBean("systemService");
	
	private int beginR;
	private int endR;
	private XSSFSheet sheet;
	
	public UploadChangeRecordThread(){}

	/**
	 * @param beginRow 开始行数
	 * @param endRow 结束行数
	 * @param sheet sheet页
	 */
	public UploadChangeRecordThread(int beginRow, int endRow, XSSFSheet sheet){
		this.beginR = beginRow;
		this.endR = endRow;
		this.sheet = sheet;
	}
	
	public void run(){
		long begin = System.currentTimeMillis();
		System.out.println("\t,开始解析："+ beginR +"\t至\t"+ endR);
		List<ZyEquipmentSpareEntity> esLists = systemService.getList(ZyEquipmentSpareEntity.class);
		List<ZyEquipmentTypeEntity> etLists = systemService.getList(ZyEquipmentTypeEntity.class);
		ZySpareRecordEntity zySpareRecordEntity = null;
		String equipmentSn = null;
		ZyEquipmentTypeEntity zyEt = null;
		SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
		List<ZySpareRecordEntity> result = new ArrayList<ZySpareRecordEntity>();
		try {
			for(int r = beginR; r <= endR; r++) {
				XSSFRow row = sheet.getRow(r);
				if(null != row){
					zySpareRecordEntity = new ZySpareRecordEntity();
					String snCellVal = ExcelUtil.getCellValue(row.getCell(0));
					for (ZyEquipmentSpareEntity es : esLists) {
						if(es.getSn().equals(snCellVal)){
							equipmentSn = es.getSn();
							break;
						}
					}
					String cell1 = ExcelUtil.getCellValue(row.getCell(1));
	        		zySpareRecordEntity.setEquipmentSn(equipmentSn);
	        		for (ZyEquipmentTypeEntity et : etLists) {
						if(et.getTypeDesc().equals(cell1)){
							zyEt = et;
							break;
						}
					}
	        		zySpareRecordEntity.setSpareDesc(zyEt);
	        		String oldSnVal = ExcelUtil.getCellValue(row.getCell(2)).trim();//
	        		zySpareRecordEntity.setOldSpareSn(oldSnVal);
	        		String oldItemVal = ExcelUtil.getCellValue(row.getCell(3)).trim();//
	        		zySpareRecordEntity.setOldSpareNo(oldItemVal);
	        		zySpareRecordEntity.setReplaceDate(ExcelUtil.getCellValue(row.getCell(4)).equals("")?null:ft.parse(ExcelUtil.getCellValue(row.getCell(4))));
	        		String newSnVal = ExcelUtil.getCellValue(row.getCell(5)).trim();//
	        		zySpareRecordEntity.setNewSpareSn(newSnVal);
	        		String newItemVal = ExcelUtil.getCellValue(row.getCell(6)).trim();//
	        		zySpareRecordEntity.setNewSpareNo(newItemVal);
	        		zySpareRecordEntity.setReplaceReason(ExcelUtil.getCellValue(row.getCell(7)));
	        		zySpareRecordEntity.setRemark(ExcelUtil.getCellValue(row.getCell(8)));
	        		zySpareRecordEntity.setRelationServiceNo(ExcelUtil.getCellValue(row.getCell(9)));
	        		result.add(zySpareRecordEntity);
	        		//更新设备的备件
	        		//systemService.executeSql("UPDATE zy_equipment_spare SET sn = ?,item = ? WHERE top_item_sn = ? AND sn = ? AND item = ? ",newSnVal,newItemVal,equipmentSn,oldSnVal,oldItemVal);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		systemService.batchSave(result);
		long end = System.currentTimeMillis();
		System.out.println("设备Excel 解析并比对 sn 完毕，用时："+(end - begin) / 1000 +"." + (end - begin) % 1000 +"秒");
	}

	public static SystemService getSystemService() {
		return systemService;
	}
	public static void setSystemService(SystemService systemService) {
		UploadChangeRecordThread.systemService = systemService;
	}

	public int getBeginR() {
		return beginR;
	}
	public void setBeginR(int beginR) {
		this.beginR = beginR;
	}

	public int getEndR() {
		return endR;
	}
	public void setEndR(int endR) {
		this.endR = endR;
	}

	public XSSFSheet getSheet() {
		return sheet;
	}
	public void setSheet(XSSFSheet sheet) {
		this.sheet = sheet;
	}
	
	
	
	
}
